package com.ruoyi.project.common;


import com.github.pagehelper.PageInfo;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.domain.FinBaseEntity;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.domainenum.PfFileType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.vo.FileVo;
import org.apache.commons.lang3.RandomStringUtils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author abel lee
 * @create 2020-10-22 09:36
 **/
public class StaticMethod {


    public static Date genTimeoutTime(int effectiveDays) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        return DateUtils.addDays(calendar.getTime(), effectiveDays - 1);
    }

    public static long calculateDaysGap(Date start, Date end) {
        return Math.abs(end.getTime() / StaticField.ONE_DAY_MILS - start.getTime() / StaticField.ONE_DAY_MILS);
    }

    public static void setCreateField(FinBaseEntity baseEntity,Long userId,String createBy){
        baseEntity.setCreateId(userId);
        baseEntity.setCreateBy(createBy);
        baseEntity.setCreateTime(DateUtils.getNowDate());
        baseEntity.setUpdateId(userId);
        baseEntity.setUpdateBy(createBy);
        baseEntity.setCreateTime(DateUtils.getNowDate());
    }

    public static void setUpdateField(FinBaseEntity baseEntity,Long userId,String createBy){
        baseEntity.setUpdateId(userId);
        baseEntity.setUpdateBy(createBy);
        baseEntity.setCreateTime(DateUtils.getNowDate());
    }

    public static PageInfo getDataTable(List<?> list)
    {
        PageInfo<?> pageInfo = new PageInfo(list);
        return pageInfo;
    }


    public static Date getStartDayTime(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        //一天的开始时间 yyyy:MM:dd 00:00:00
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }


    public static Date getEndDayTime(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        //一天的开始时间 yyyy:MM:dd 00:00:00
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 58);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    public static Date genTimeoutTimeByHours(Float effectiveHours) {
        Calendar calendar = Calendar.getInstance();
        System.out.println("add hours " + (int) (effectiveHours / 1));
        calendar.add(Calendar.HOUR_OF_DAY, (int) (effectiveHours / 1));
        if (effectiveHours % 1 > 0) {
            calendar.add(Calendar.MINUTE, (int) (effectiveHours % 1 * 60));
            System.out.println("add MINUTE " + (int) (effectiveHours % 1 * 60));

        }
        return calendar.getTime();
    }

    public static String formatMobilePhone(String mobilePhone) {
        if (StringUtils.isNotBlank(mobilePhone) && mobilePhone.length() == 11) {
            return mobilePhone.substring(0, 3) + "****" + mobilePhone.substring(7);
        } else {
            return "";
        }
    }

    public static String genYearAndMonth(int year,int month){
        if(month<10){
            return year+"-0"+month;
        }else {
            return year + "-" + month;
        }
    }


    public static void main(String args[]) {
        System.out.println(formatMobilePhone("18190910296"));
    }


    public static String imageFileVoListToString(List<FileVo> imageFileVoList) {
        String result = "";
        if (imageFileVoList != null && imageFileVoList.size() > 0) {
            int size = imageFileVoList.size();
            FileVo imageFileVo;
            for (int i = 0; i < size; i++) {
                imageFileVo = imageFileVoList.get(i);
                if (StringUtils.isNotBlank(imageFileVo.getUrl())) {
                    result = result + imageFileVo.getUrl() + (i == (size - 1) ? "" : StaticField.IMAGE_ARRAY_SPLIT);
                }
            }
        }
        return result;
    }

    public static List<FileVo> stringToImageFileVoList(String imageFileStr) {
        List<FileVo> result = new ArrayList<>();
        if (StringUtils.isNotBlank(imageFileStr)) {
            String[] urlArray = imageFileStr.split("\\" + StaticField.IMAGE_ARRAY_SPLIT);
            FileVo imageFileVo = null;
            for (int i = 0; i < urlArray.length; i++) {
                if (StringUtils.isNotBlank(urlArray[i])) {
                    imageFileVo = new FileVo();
                    String name = urlArray[i].substring(urlArray[i].lastIndexOf("/") + 1);
                    imageFileVo.setName(name);
                    imageFileVo.setOrderNum(i * 10);
                    imageFileVo.setUrl(urlArray[i]);
                    imageFileVo.setFileType(genFileTypeFromName(name));
                    result.add(imageFileVo);
                }
            }
        }
        return result;
    }

    public static String[] stringToImageUrlArray(String imageFileStr) {
        if (StringUtils.isNotBlank(imageFileStr)) {
            return imageFileStr.split("\\" + StaticField.IMAGE_ARRAY_SPLIT);
        }
        return null;
    }

    public static FileVo stringToImageFileVo(String imageFileStr) {
        FileVo fileVo = null;
        if (StringUtils.isNotBlank(imageFileStr)) {
            fileVo = new FileVo();
            String name = imageFileStr.substring(imageFileStr.lastIndexOf("/"));
            fileVo.setName(name);
            fileVo.setOrderNum(0);
            fileVo.setUrl(imageFileStr);
            fileVo.setFileType(genFileTypeFromName(name));
        }
        return fileVo;
    }

    public static PfFileType genFileTypeFromName(String name) {
        if (StringUtils.isNotBlank(name) && name.contains(".")) {
            String filenameExtension = name.substring(name.lastIndexOf(".")).toLowerCase();
            if (filenameExtension.equals(".bmp") || filenameExtension.equals(".GIF") || filenameExtension.equals(".gif")) {
                return PfFileType.IMAGE;
            }
            if (filenameExtension.equals(".jpeg") || filenameExtension.equals(".jpg") || filenameExtension.equals(".png")) {
                return PfFileType.IMAGE;
            }
            if (filenameExtension.equals(".mp4") || filenameExtension.equals(".m3u8") || filenameExtension.equals(".flv")) {
                return PfFileType.VIDEO;
            }
        }
        return PfFileType.IMAGE;
    }

    public static List<String> stringToActiveTagList(String userTag) {
        List<String> result = new ArrayList<>();
        if (StringUtils.isNotBlank(userTag)) {
            String[] urlArray = userTag.split("\\" + StaticField.TAG_SPLIT);
            for (int i = 0; i < urlArray.length; i++) {
                if (StringUtils.isNotBlank(urlArray[i])) {
                    result.add(urlArray[i].replaceAll(StaticField.USER_TAG_PREFIX_AND_END, ""));
                }
            }
        }
        return result;
    }


    public static String genVerificationCode() {
        StringBuilder builder = new StringBuilder("");
        builder.append(RandomStringUtils.random(5, "0123456789"));
        builder.append("-" + RandomStringUtils.random(5, "0123456789"));
        builder.append("-" + RandomStringUtils.random(5, "0123456789"));
        return builder.toString();
    }

    public static String genPayNo() {
        return DateUtils.dateTimeNow("yyyyMMddHHMMSS") + RandomStringUtils.random(5, "0123456789");
    }

    public static String genOrderNo() {
        return DateUtils.dateTimeNow("yyyyMMddHHMMSS") + RandomStringUtils.random(5, "0123456789");
    }

    public static boolean checkLongValueId(Long value) {
        return value != null && value.longValue() > 0;
    }

    public static boolean idEquals(Long idOne, Long idTwo) {
        boolean result = false;
        if (idOne != null && idTwo != null && idOne.longValue() == idTwo.longValue()) {
            result = true;
        }
        return result;
    }

}
